﻿'图形属性
Public Structure FigureProperty
    Dim clrForeColor As Color           '前景色
    Dim clrBackColor As Color           '背景色
    Dim nLineWidth As Integer           '线宽
End Structure

'图形元素基类
Public Class FigureElement
    Inherits renderer

    Sub New(rect As Rectangle)
        MyBase.new()
        SetRect(rect)
        m_Prop = New FigureProperty
        SetProp(Color.Black, Color.White, 1)
        m_bSelected = False
    End Sub

    Sub New(x As Integer, y As Integer, w As Integer, h As Integer)
        MyBase.new()
        SetRect(x, y, w, h)
        m_Prop = New FigureProperty
        SetProp(Color.Black, Color.White, 1)
        m_bSelected = False
    End Sub

    Sub New()
        MyBase.new()
        m_Prop = New FigureProperty
        SetProp(Color.Black, Color.White, 1)
        m_bSelected = False
    End Sub

    Public Overridable Sub SetRect(rect As Rectangle)
        If m_BaseRect = Nothing Then
            m_BaseRect = New Rectangle
        End If

        m_BaseRect = rect
    End Sub

    Public Sub SetRect(x As Integer, y As Integer, w As Integer, h As Integer)
        If m_BaseRect = Nothing Then
            m_BaseRect = New Rectangle
        End If

        m_BaseRect.X = x
        m_BaseRect.Y = y
        m_BaseRect.Width = w
        m_BaseRect.Height = h
    End Sub

    Public Sub SetProp(fore As Color, back As Color, nLine As Integer)
        m_Prop.clrForeColor = fore
        m_Prop.clrBackColor = back
        m_Prop.nLineWidth = nLine
        m_Pen = New Pen(m_Prop.clrForeColor, m_Prop.nLineWidth)
        m_Brush = New SolidBrush(m_Prop.clrBackColor)
        'main.pnlMainClient.Invalidate(m_BaseRect)
    End Sub

    Public Sub SetPropEx(fore As Color, back As Color, nLine As Integer)
        SetProp(fore, back, nLine)
        m_Parent.RenderEx()
    End Sub

    Public Function GetProp() As FigureProperty
        Return m_Prop
    End Function

    Public Function GetRect() As Rectangle
        Return m_BaseRect
    End Function

    Public Overrides Sub RenderEx()
        m_Parent.RenderEx()
    End Sub

    Public Sub SetParent(ByRef canvas As Canvas)
        m_Parent = canvas
    End Sub

    Public Function GetParent() As Canvas
        Return m_Parent
    End Function

    Public Sub Move(point As Point)
        m_BaseRect.Location = point
    End Sub

    Public Sub Move(x As Integer, y As Integer)
        m_BaseRect.X = x
        m_BaseRect.Y = y
    End Sub

    Public Sub MoveEx(point As Point)
        Move(point)
        m_Parent.RenderEx()
    End Sub

    Public Sub SetSelected(flag As Boolean)
        m_bSelected = flag
        'm_bDrag = flag
    End Sub

    Public Function IsSelected() As Boolean
        Return m_bSelected
    End Function

    Protected m_Prop As FigureProperty
    Protected m_BaseRect As Rectangle
    Protected m_Parent As Canvas
    Private m_bSelected As Boolean
    Private m_bDrag As Boolean
End Class


'画板
Public Class Canvas
    Inherits CanvasRenderer

    Sub New()
        MyBase.New()
        m_FigureManager = New FigureManager
        m_FigureManager.Init()
        m_FigureManager.SetParent(Me)
        m_BackColor = New Color

        '默认背景画面颜色
        m_BackColor = Color.AliceBlue
        m_BackPen = New Pen(m_BackColor)
        m_BackBrush = New SolidBrush(m_BackColor)
    End Sub

    Public Sub SetBackColor(color As Color)
        m_BackColor = color
        'main.pnlMainClient.BackColor = m_BackColor
        m_BackPen = New Pen(m_BackColor, 0)
        m_BackBrush = New SolidBrush(m_BackColor)
    End Sub

    Public Sub SetBackColorEx(color As Color)
        SetBackColor(color)
        Me.RenderEx()
    End Sub

    Public Function GetBackColor() As Color
        Return m_BackColor
    End Function

    Public Overrides Sub BufferRender()
        MyBase.BufferRender()

        m_BufferDC.FillRectangle(m_BackBrush, m_CanvasRect)
        m_BufferDC.DrawRectangle(m_BackPen, m_CanvasRect)

        Dim i As Integer = 0
        Dim fg As FigureElement

        While (i < m_FigureManager.m_FigureList.Count)
            fg = m_FigureManager.m_FigureList.Item(i)
            fg.BufferRender()
            i = i + 1
        End While
    End Sub

    Public Sub Add(ByRef figure As FigureElement)
        m_FigureManager.Add(figure)
        figure.SetParent(Me)
    End Sub

    Protected Overrides Sub paint(ByRef sender As Object, ByRef e As System.Windows.Forms.PaintEventArgs)

        BufferRender()

        MyBase.paint(sender, e)
    End Sub


    Public Sub SetRect(ByRef rect As Rectangle)
        m_CanvasRect = rect
    End Sub

    Public Sub SetRect(x As Integer, y As Integer, w As Integer, h As Integer)
        m_CanvasRect.X = x
        m_CanvasRect.Y = y
        m_CanvasRect.Width = w
        m_CanvasRect.Height = h
    End Sub

    Public Function GetRect() As Rectangle
        Return m_CanvasRect
    End Function

    Private m_BackColor As Color
    Private m_CanvasRect As Rectangle
    Public m_FigureManager As FigureManager

    Private m_BackPen As Pen
    Private m_BackBrush As Brush
End Class


'矩形
Public Class FigureRect
    Inherits FigureElement

    Sub New()

    End Sub

    Sub New(rect As Rectangle)
        MyBase.New(rect)
    End Sub

    Sub New(x As Integer, y As Integer, w As Integer, h As Integer)
        MyBase.New(x, y, w, h)
    End Sub

    Public Overrides Sub BufferRender()
        MyBase.BufferRender()

        m_BufferDC.FillRectangle(m_Brush, m_BaseRect)
        m_BufferDC.DrawRectangle(m_Pen, m_BaseRect)
    End Sub

End Class


'圆
Public Class FigureEllipse
    Inherits FigureElement

    Sub New()

    End Sub

    Sub New(rect As Rectangle)
        MyBase.New(rect)
    End Sub

    Sub New(x As Integer, y As Integer, w As Integer, h As Integer)
        MyBase.New(x, y, w, h)
    End Sub

    Public Overrides Sub BufferRender()
        MyBase.BufferRender()

        m_BufferDC.FillEllipse(m_Brush, m_BaseRect)
        m_BufferDC.DrawEllipse(m_Pen, m_BaseRect)
    End Sub

End Class

'图片
Public Class FigureImage
    Inherits FigureElement

    Sub New()
        MyBase.New()
        'm_Image = New Bitmap(0, 0)
    End Sub

    Sub New(rect As Rectangle)
        MyBase.New(rect)
        'm_Image = New Bitmap(0, 0)
    End Sub

    Sub New(x As Integer, y As Integer, w As Integer, h As Integer)
        MyBase.New(x, y, w, h)
        'm_Image = New Bitmap(0, 0)
    End Sub

    Public Overrides Sub BufferRender()
        MyBase.BufferRender()

        m_BufferDC.DrawImage(m_Image, m_BaseRect)
    End Sub

    Public Sub SetImage(path As String)

        m_ImagePath = path
        m_Image = Image.FromFile(m_ImagePath)

        SetRect(m_BaseRect.X, m_BaseRect.Y, m_Image.Width, m_Image.Height)
    End Sub

    Public Sub SetImageEx(path As String)
        SetImage(path)
        m_Parent.RenderEx()
    End Sub

    Public Function GetImagePath() As String
        Return m_ImagePath
    End Function


    Private m_ImagePath As String
    Private m_Image As Bitmap
End Class